<template>
  <!-- // 分配角色 -->
  <div>
    <!-- 这里将来会放置多选列表 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="submit">确定</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings'
import { getStaffInfo } from '@/api/user'
import { assignRoles } from '@/api/employee'
export default {
  props: {
    employeeId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      roleList: []// 角色列表
    }
  },
  created() { this.loadRole() },
  methods: {
    // 通知父组件关闭弹窗
    closeDialog() {
      this.$emit('close')
    },
    // 加载角色
    async loadRole() {
      try {
        const res = await getRoles({ page: 1, pagesize: 100 })
        console.log(res, '封神榜')
        this.roleList = res.data.rows
        this.loadEmployeeInfo()
      } catch (error) {
        console.log(error)
      }
    },
    // 获取员工基本信息
    async loadEmployeeInfo() {
      try {
        const res = await getStaffInfo(this.employeeId)
        // console.log('getStaffInfo', res)
        console.log(this.employeeId, '是一个东西吗')
        this.roleIds = res.data.roleIds || []
        console.log(this.roleIds, 'this.roleIds')
      } catch (err) {
        console.log('getStaffInfo', err)
      }
    },
    // 分配角色
    async giveRole() {
      try {
        const res = await assignRoles({ id: this.employeeId, roleIds: this.roleIds })
        console.log('assignRoles', res)
        // 保存数据
      } catch (err) {
        console.log('assignRoles', err)
      }
    },
    // 提交
    submit() {
      this.closeDialog()
      this.giveRole()
    },
    // 取消
    cancel() {
      this.closeDialog()
    }
  }
}
</script>
